package com.future.enable;

import com.future.anno.EnableDoAsync;
import org.springframework.aop.config.AopConfigUtils;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.context.annotation.ImportBeanDefinitionRegistrar;
import org.springframework.core.annotation.AnnotationAttributes;
import org.springframework.core.type.AnnotationMetadata;

/**
 * Import注解注入Bean的方式有如下三种
 * implements ImportBeanDefinitionRegistrar
 * 开启AOP的处理，以及Spring在日志处理过程中使用的代理方式
 * 暂不启用
 */
public class AsyncImportBeanDefinitionRegistrar implements ImportBeanDefinitionRegistrar {

    @Override
    public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry) {

        AnnotationAttributes attributes = AnnotationAttributes.fromMap(
                importingClassMetadata.getAnnotationAttributes(EnableDoAsync.class.getName(), false));
        boolean proxyTargetClass = attributes.getBoolean("proxyTargetClass");
        AopConfigUtils.registerAutoProxyCreatorIfNecessary(registry);
        if (proxyTargetClass) {
            AopConfigUtils.forceAutoProxyCreatorToUseClassProxying(registry);
        }

    }

}
